<?php

namespace app\rongcui\controller;

use app\rongcui\model\AreasModel;
use app\rongcui\model\CustomerNatureModel;
use app\rongcui\model\RongcuiFilesModel;
use app\rongcui\model\RongcuiIndustryCateModel;
use app\rongcui\model\RongcuiPotentialCustomerModel;
use app\rongcui\model\RongcuiFollowModel;
use cmf\controller\AdminBaseController;

class ResourceLibraryController extends AdminBaseController
{
    function index()
    {
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();

        $where = [
            'potential.type' => 0,
            'lock_status' => 2,
        ];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['lock_uid'] = ['in', $users];
        } else {
            $where['lock_uid'] = ['neq', 0];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['company|address|user.user_nickname|puser.user_nickname'] = ['like', "%$keyword%"];
//            $where['company|corporation|address|email|phone|remark|scope'] = ['like', "%$keyword%"];
        }
        $cate_id = input('category','-1');
        if($cate_id != -1) {
            $where['cate_id'] = $cate_id;
        }
        if (input('attorn')) {
            $where['attorn'] = input('attorn');
        }
        if (input('min_registered_capital')) {
            $where['registered_capital'] = ['egt',input('min_registered_capital')];
        }
        if (input('max_registered_capital')) {
            if(isset($where['registered_capital'])) {
                $where['registered_capital'] = ['between',[input('min_registered_capital'),input('max_registered_capital')]];
            }else{
                $where['registered_capital'] = ['elt',input('max_registered_capital')];
            }
        }
        if (input('regtime')) {
            $regtime = input('regtime');
            switch ($regtime) {
                case 1:
                    $where['regtime'] = ['egt',strtotime('-1 year')];
                    break;
                case 2:
                    $where['regtime'] = ['between',[strtotime('-2 year'),strtotime('-1 year')]];
                    break;
                case 3:
                    $where['regtime'] = ['between',[strtotime('-3 year'),strtotime('-2 year')]];
                    break;
                case 4:
                    $where['regtime'] = ['between',[strtotime('-5 year'),strtotime('-3 year')]];
                    break;
                case 5:
                    $where['regtime'] = ['between',[strtotime('-10 year'),strtotime('-5 year')]];
                    break;
                case 6:
                    $where['regtime'] = ['elt',strtotime('-10 year')];
                    break;
            }
        }

        if (input('day')) {
            $day = input('day', 0, 'intval');
            if ($day) {
                $time = strtotime("-{$day} day");
                $where['newtime'] = ['lt', $time];
            }
//            $where['company|corporation|address|email|phone|remark|scope'] = ['like', "%$keyword%"];
        }

        $lists = $potentialCustomerModel
            ->alias('potential')
            ->join('__CURRENCY__ currency', 'potential.currency = currency.id')
            ->join('__USER__ user', 'potential.lock_uid = user.id')
            ->join('__USER__ puser', 'potential.uid = puser.id')
            ->join('__CUSTOMER_NATURE__ nature', 'potential.attorn = nature.id','left')
            ->field('potential.*,currency.name cname,user.user_nickname,nature.name as aname,puser.user_nickname pname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $customerNatureModel = new CustomerNatureModel();
        $attorn = $customerNatureModel->where([
            'type' => 9,
            'is_del' => 0
        ])->field('id,name')->select();

        $industryCateModel = new RongcuiIndustryCateModel();
        $cate = $industryCateModel->where([
            'status' => 1,
            'is_del' => 0
        ])->order('parent_id,list_order,id')->select();

        $this->assign('where_r', $where_r);
        $this->assign('cate', $cate);
        $this->assign('lists', $lists);
        $this->assign('attorn', $attorn);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function follow()
    {
        $pid = $this->request->param('pid', 0, 'intval');
        if (!$pid) {
            $this->error('非法访问');
        }

        $followModel = new RongcuiFollowModel();
        $where = ['type' => 3, 'pid' => $pid];
        $lists = $followModel
            ->alias('follow')
            ->join('__USER__ user', 'follow.uid = user.id')
            ->field('follow.*,user.user_nickname as uname')
            ->where($where)
            ->order(['id desc'])
            ->paginate(10);
        $page = $lists->render();

        $this->assign('lists', $lists);
        $this->assign('page', $page);
        $this->assign('pid', $pid);
        return $this->fetch();
    }

    function lock(){
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('非法访问');
        }
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $info = $potentialCustomerModel
            ->where([
                'id' => $id,
                'lock_uid' => 0
            ])
            ->find();
        if(!$info){
            $this->error('已被锁定！');
        }

        if($this->request->isPost()) {
            $arrData = $this->request->post();
            $arrData['lock_uid'] = cmf_get_current_admin_id();
            $arrData['lock_status'] = 2;
            $arrData['lock_time'] = time();

            $result = $potentialCustomerModel->allowField(true)->save($arrData,[
                'id' => $id,
                'lock_uid' => 0
            ]);
            if($result !== false) {
                $this->success('已锁定！',url('index'));
            }else{
                $this->error($potentialCustomerModel->getError());
            }
        }

        $customerNatureModel = new CustomerNatureModel();
        $lists = $customerNatureModel->where([
            'type' => 9
        ])->order('list_order,id')->select();

        $this->assign('result',$info);
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    function lock_lists(){
        $where = [
            'lock_status' => 1,
//            [
//            'lock_status' => [
//                'neq', 0
//            ],
        ];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['lock_uid'] = ['in', $users];
        } else {
            $where['lock_uid'] = ['neq', 0];
        }

        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $lists = $potentialCustomerModel
            ->alias('potential')
            ->join('__CURRENCY__ currency', 'potential.currency = currency.id')
            ->join('__USER__ user', 'potential.lock_uid = user.id')
            ->field('potential.*,currency.name cname,user.user_nickname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $cate_id = $this->request->param('cate_id',0,'intval');
        $cate = [
            'other',
            'bxjj',
            'qgbxdl',
            'qybxdl',
            'rzdb',
            'xed',
            'ddh',
        ];
        $this->assign('index_url',url('potential_customer/'.$cate[$cate_id]));
        $this->assign('lists', $lists);
        $this->assign('page', $page);
        return $this->fetch();
    }

    function lock_se(){
        $pid = $this->request->param('id', 0, 'intval');
        if (!$pid) {
            $this->error('非法访问');
        }

        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $info = $potentialCustomerModel
            ->where([
                'lock_status' => 1
            ])
            ->find();
        if ($info) {
            $result = $potentialCustomerModel->save([
                'lock_status' => 2,
                'lock_time' => time(),
            ], [
                'id' => $pid,
                'lock_status' => 1,
            ]);
            if ($result !== false) {
                $this->success('锁定成功！');
            }else{
                $this->error('该数据已处理！');
            }
        }else{
            $this->error('该数据已处理！');
        }
    }

    function add_follow()
    {
        $pid = $this->request->param('pid', 0, 'intval');
        if (!$pid) {
            $this->error('非法访问');
        }
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $info = $potentialCustomerModel
            ->where([
                'id' => $pid
            ])
            ->find();
        if ($this->request->isPost()) {
            $followModel = new RongcuiFollowModel();
            $arrData = $this->request->post();

            $time = time();
            $arrData['create_time'] = $time;
            $arrData['type'] = 3;
            $arrData['uid'] = cmf_get_current_admin_id();
            $arrData['next_time'] = strtotime($arrData['next_time']);
            if ($arrData['next_time'] < $time) {
                $arrData['next_time'] = 0;
            }
            if (!$arrData['pid']) {
                $this->error('非法访问');
            }
            if(isset($arrData['attorn']) && $arrData['attorn'] != $info['attorn']) {
                $potentialCustomerModel->save(['attorn' => $arrData['attorn']],['id' => $pid]);
            }

            $result = $this->validate($arrData, 'Follow');
            if ($result !== true) {
                $this->error($result);
            }

            $result = $followModel->allowField(true)->save($arrData);
            if ($result) {
                $potentialCustomerModel->where(['id' => $arrData['pid']])->update([
                    'evolve' => $arrData['remark'],
                    'newtime' => $time,
                    'nexttime' => $arrData['next_time']
                ]);
            }

            $this->success(lang("ADD_SUCCESS"), url("follow", ['pid' => $arrData['pid']]));
        }

        $customerNatureModel = new CustomerNatureModel();
        $lists = $customerNatureModel->where([
            'type' => 9
        ])->order('list_order,id')->select();

        $this->assign('pid', $pid);
        $this->assign('result', $info);
        $this->assign('lists', $lists);
        return $this->fetch();
    }


    function edit_follow()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('非法访问');
        }
        if ($this->request->isPost()) {
            $followModel = new RongcuiFollowModel();
            $arrData = $this->request->post();

            $time = time();
            $arrData['next_time'] = strtotime($arrData['next_time']);
            if ($arrData['next_time'] < $time) {
                $arrData['next_time'] = 0;
            }

            $result = $this->validate($arrData, 'Follow.edit');
            if ($result !== true) {
                $this->error($result);
            }

            $result = $followModel->allowField(true)->where(["id" => $arrData["id"]])->update($arrData);
            if ($result) {
                $new = $followModel->where(['type' => 3, 'pid' => $arrData['pid']])->max('id');
                if ($new == $arrData['id']) {
                    $newData = [
                        'evolve' => $arrData['remark'],
                        'newtime' => $time,
                        'nexttime' => $arrData['next_time'],
                    ];
                    if(isset($arrData['attorn'])) {
                        $newData['attorn'] = $arrData['attorn'];
                    }
                    $potentialCustomerModel = new RongcuiPotentialCustomerModel();
                    $potentialCustomerModel->where(['id' => $arrData['pid']])->update($newData);
                }
            }

            $this->success(lang("EDIT_SUCCESS"), url("follow", ['pid' => $arrData['pid']]));
        }
        $followModel = new RongcuiFollowModel();
        $result = $followModel->where(['id' => $id])->find();

        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $info = $potentialCustomerModel->find($result['pid']);

        $customerNatureModel = new CustomerNatureModel();
        $lists = $customerNatureModel->where([
            'type' => 9
        ])->order('list_order,id')->select();

        $this->assign('result', $result);
        $this->assign('info', $info);
        $this->assign('lists', $lists);
        return $this->fetch();
    }

    function unlock()
    {
        $id = $this->request->param('id', 0, 'intval');
        $ids = $this->request->param('ids/a', []);
        if (!$id && !$ids) {
            $this->error('非法访问');
        }
        $ids[] = $id;
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $potentialCustomerModel->save([
            'lock_uid' => 0,
            'lock_status' => 0,
        ], ['id' => ['in',$ids]]);
        $this->success('解锁成功！');
    }

    function unlock_se()
    {
        $id = $this->request->param('id', 0, 'intval');
        $ids = $this->request->param('ids/a', []);
        if (!$id && !$ids) {
            $this->error('非法访问');
        }
        $ids[] = $id;
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $potentialCustomerModel->save([
            'type' => 0,
            'product_sh' => 0,
        ], ['id' => ['in',$ids]]);
        db('files')->where([
            'is_del' => 0,
            'pid' => ['in',$ids]
        ])->setField('is_del',1);
        $this->success('解锁成功！');
    }

    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('非法访问');
        }
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        if ($this->request->isPost()) {
            $arrData = $this->request->param();

            if(isset($arrData['shi']) && $arrData['shi']){
                $arrData['areas'] = $arrData['shi'];
            }
            if (!empty($arrData['file_names']) && !empty($arrData['file_urls'])) {
                $arrData['files'] = [];
                foreach ($arrData['file_urls'] as $key => $url) {
                    $fileUrl = cmf_asset_relative_url($url);
                    array_push($arrData['files'], ["url" => $fileUrl, "name" => $arrData['file_names'][$key]]);
                }
                $arrData['files'] = json_encode($arrData['files']);
            }

            if(isset($arrData['type']) && $arrData['type']) {
                $arrData['product_sh'] = 1;
                $arrData['product_time'] = time();
            }
            $result = $potentialCustomerModel->allowField(true)->validate(true)->save($arrData, ['id' => $id]);
            if ($result !== false) {
                $this->success('保存成功！', url('product_lists'));
            } else {
                $this->error($potentialCustomerModel->getError());
            }
        }
        $result = $potentialCustomerModel->where(['id' => $id])->find();
        $result['files'] = json_decode($result['files'], true);


        $customerNatureModel = new CustomerNatureModel();
        $lists = $customerNatureModel->where([
            'type' => 9
        ])->order('list_order,id')->select();

        $areasModel = new AreasModel();
        $areas = $areasModel
            ->where([
                'level' => 1
            ])
            ->select();

        $this->assign('result', $result);
        $this->assign('lists', $lists);
        $this->assign('areas', $areas);
        return $this->fetch();
    }

    function product_lists(){
        $where = [
            'product_sh' => 1
//            'product_sh' => [
//                'neq', 0
//            ],
        ];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['lock_uid'] = ['in', $users];
        } else {
            $where['lock_uid'] = ['neq', 0];
        }

        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $lists = $potentialCustomerModel
            ->alias('potential')
            ->join('__CURRENCY__ currency', 'potential.currency = currency.id')
            ->join('__USER__ user', 'potential.lock_uid = user.id')
            ->field('potential.*,currency.name cname,user.user_nickname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('lists', $lists);
        $this->assign('page', $page);
        return $this->fetch();
    }

    function shtg() {
        $pid = $this->request->param('id', 0, 'intval');
        if (!$pid) {
            $this->error('非法访问');
        }

        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $info = $potentialCustomerModel
            ->where([
                'product_sh' => 1,
                'id' => $pid,
            ])
            ->find();
        if ($info) {

            $files = json_decode($info['files'],true);
            $data = [];
            if($files) {
                foreach ($files as $file) {
                    $data[] = [
                        'file_name' => $file['name'],
                        'file' => $file['url'],
                        'uid' => $info['lock_uid'],
                        'pid' => $pid,
                        'type' => 1,
                    ];
                }
            }
            $result = $potentialCustomerModel->save([
                'product_sh' => 2,
            ], [
                'id' => $pid,
                'product_sh' => 1,
            ]);
            if ($result !== false) {
                if($files) {
                    $rongcuiFilesModel = new RongcuiFilesModel();
                    $rongcuiFilesModel->where([
                        'pid' => $pid,
                        'is_del' => 0,
                    ])->setField('is_del',1);

                    $rongcuiFilesModel->allowField(true)->saveAll($data);
                }

                $this->success('审核成功！');
            }else{
                $this->error('该数据已处理！');
            }
        }else{
            $this->error('该数据已处理！');
        }
    }

    function product()
    {
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();

        $where = [
            'potential.type' => ['neq', 0],
            'done' => 0,
            'product_sh' => 2,
        ];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['lock_uid'] = ['in', $users];
        } else {
            $where['lock_uid'] = ['neq', 0];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['company|address|user.user_nickname|puser.user_nickname'] = ['like', "%$keyword%"];
//            $where['company|corporation|address|email|phone|remark|scope'] = ['like', "%$keyword%"];
        }
        $cate_id = input('category','-1');
        if($cate_id != -1) {
            $where['cate_id'] = $cate_id;
        }
        if (input('attorn')) {
            $where['attorn'] = input('attorn');
        }
        if (input('min_registered_capital')) {
            $where['registered_capital'] = ['egt',input('min_registered_capital')];
        }
        if (input('max_registered_capital')) {
            if(isset($where['registered_capital'])) {
                $where['registered_capital'] = ['between',[input('min_registered_capital'),input('max_registered_capital')]];
            }else{
                $where['registered_capital'] = ['elt',input('max_registered_capital')];
            }
        }
        if (input('regtime')) {
            $regtime = input('regtime');
            switch ($regtime) {
                case 1:
                    $where['regtime'] = ['egt',strtotime('-1 year')];
                    break;
                case 2:
                    $where['regtime'] = ['between',[strtotime('-2 year'),strtotime('-1 year')]];
                    break;
                case 3:
                    $where['regtime'] = ['between',[strtotime('-3 year'),strtotime('-2 year')]];
                    break;
                case 4:
                    $where['regtime'] = ['between',[strtotime('-5 year'),strtotime('-3 year')]];
                    break;
                case 5:
                    $where['regtime'] = ['between',[strtotime('-10 year'),strtotime('-5 year')]];
                    break;
                case 6:
                    $where['regtime'] = ['elt',strtotime('-10 year')];
                    break;
            }
        }
        if (input('day')) {
            $day = input('day', 0, 'intval');
            if ($day) {
                $time = strtotime("-{$day} day");
                $where['newtime'] = ['lt', $time];
            }
//            $where['company|corporation|address|email|phone|remark|scope'] = ['like', "%$keyword%"];
        }

        $lists = $potentialCustomerModel
            ->alias('potential')
            ->join('__CURRENCY__ currency', 'potential.currency = currency.id')
            ->join('__USER__ user','user.id = potential.lock_uid')
            ->join('__USER__ puser', 'potential.uid = puser.id')
            ->join('__CUSTOMER_NATURE__ nature', 'potential.attorn = nature.id','left')
            ->field('potential.*,currency.name cname,user.user_nickname,nature.name as aname,puser.user_nickname pname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $customerNatureModel = new CustomerNatureModel();
        $attorn = $customerNatureModel->where([
            'type' => 9,
            'is_del' => 0
        ])->field('id,name')->select();

        $industryCateModel = new RongcuiIndustryCateModel();
        $cate = $industryCateModel->where([
            'status' => 1,
            'is_del' => 0
        ])->order('parent_id,list_order,id')->select();

        $this->assign('where_r', $where_r);
        $this->assign('lists', $lists);
        $this->assign('attorn', $attorn);
        $this->assign('cate', $cate);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * 成交
     */
    public function done()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('非法访问');
        }
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $potentialCustomerModel->save([
            'done' => 1,
            'donesq' => 1,
            'done_time' => time()
        ], ['id' => $id]);
        $this->success('成交申请提交完成！',cmf_url('donesq_lists'));
    }

    function donesq_lists(){
        $where = [
            'donesq' => 1
//            'donesq' => [
//                'neq', 0
//            ],
        ];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['lock_uid'] = ['in', $users];
        } else {
            $where['lock_uid'] = ['neq', 0];
        }

        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $lists = $potentialCustomerModel
            ->alias('potential')
            ->join('__CURRENCY__ currency', 'potential.currency = currency.id')
            ->join('__USER__ user', 'potential.lock_uid = user.id')
            ->field('potential.*,currency.name cname,user.user_nickname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('lists', $lists);
        $this->assign('page', $page);
        return $this->fetch();
    }

    function donesq_se() {
        $pid = $this->request->param('id', 0, 'intval');
        if (!$pid) {
            $this->error('非法访问');
        }

        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $info = $potentialCustomerModel
            ->where([
                'donesq' => 1
            ])
            ->find();
        if ($info) {
            $type = $this->request->param('type',0,'intval');
            if($type) {
                $result = $potentialCustomerModel->save([
                    'done' => 0,
                    'donesq' => 0,
                ], [
                    'id' => $pid,
                    'donesq' => 1,
                ]);
                if ($result !== false) {
                    $this->success('审核成功！',cmf_url('done_lists'));
                }else{
                    $this->error('该数据已处理！');
                }
            }else{
                $result = $potentialCustomerModel->save([
                    'donesq' => 2,
                ], [
                    'id' => $pid,
                    'donesq' => 1,
                ]);
                if ($result !== false) {
                    $this->success('审核成功！',cmf_url('done_lists'));
                }else{
                    $this->error('该数据已处理！');
                }
            }
        }else{
            $this->error('该数据已处理！');
        }
    }

    public function files()
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('非法访问');
        }
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();
        $result = $potentialCustomerModel->find($id);
        if(!$result) {
            $this->error('记录不存在！');
        }
        $files = json_decode($result['files'], true);
        if($files) {
            foreach ($files as &$file) {
                $file['url'] = cmf_get_file_download_url($file['url']);
            }
        }
        $this->success('获取成功！', '', $files);
    }

    function done_lists()
    {
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();

        $where = [
            'done' => ['neq', 0],
            'donesq' => 2,
        ];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['lock_uid'] = ['in', $users];
        } else {
            $where['lock_uid'] = ['neq', 0];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['company'] = ['like', "%$keyword%"];
//            $where['company|corporation|address|email|phone|remark|scope'] = ['like', "%$keyword%"];
        }
        $cate_id = input('category','-1');
        if($cate_id != -1) {
            $where['cate_id'] = $cate_id;
        }
        if (input('attorn')) {
            $where['attorn'] = input('attorn');
        }
        if (input('min_registered_capital')) {
            $where['registered_capital'] = ['egt',input('min_registered_capital')];
        }
        if (input('max_registered_capital')) {
            if(isset($where['registered_capital'])) {
                $where['registered_capital'] = ['between',[input('min_registered_capital'),input('max_registered_capital')]];
            }else{
                $where['registered_capital'] = ['elt',input('max_registered_capital')];
            }
        }
        if (input('regtime')) {
            $regtime = input('regtime');
            switch ($regtime) {
                case 1:
                    $where['regtime'] = ['egt',strtotime('-1 year')];
                    break;
                case 2:
                    $where['regtime'] = ['between',[strtotime('-2 year'),strtotime('-1 year')]];
                    break;
                case 3:
                    $where['regtime'] = ['between',[strtotime('-3 year'),strtotime('-2 year')]];
                    break;
                case 4:
                    $where['regtime'] = ['between',[strtotime('-5 year'),strtotime('-3 year')]];
                    break;
                case 5:
                    $where['regtime'] = ['between',[strtotime('-10 year'),strtotime('-5 year')]];
                    break;
                case 6:
                    $where['regtime'] = ['elt',strtotime('-10 year')];
                    break;
            }
        }
        if (input('day')) {
            $day = input('day', 0, 'intval');
            if ($day) {
                $time = strtotime("-{$day} day");
                $where['newtime'] = ['lt', $time];
            }
//            $where['company|corporation|address|email|phone|remark|scope'] = ['like', "%$keyword%"];
        }

        $lists = $potentialCustomerModel
            ->alias('potential')
            ->join('__CURRENCY__ currency', 'potential.currency = currency.id')
            ->field('potential.*,currency.name cname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $customerNatureModel = new CustomerNatureModel();
        $attorn = $customerNatureModel->where([
            'type' => 9,
            'is_del' => 0
        ])->field('id,name')->select();

        $industryCateModel = new RongcuiIndustryCateModel();
        $cate = $industryCateModel->where([
            'status' => 1,
            'is_del' => 0
        ])->order('parent_id,list_order,id')->select();

        $this->assign('where_r', $where_r);
        $this->assign('lists', $lists);
        $this->assign('attorn', $attorn);
        $this->assign('cate', $cate);
        $this->assign('page', $page);
        return $this->fetch();
    }

    function count_lists()
    {
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();

        $where = [
            'user_status' => ['neq',0]
        ];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['lock_uid'] = ['in', $users];
        } else {
            $where['lock_uid'] = ['neq', 0];
        }
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['user_nickname'] = ['like', "%$keyword%"];
//            $where['company|corporation|address|email|phone|remark|scope'] = ['like', "%$keyword%"];
        }

        $lists = $potentialCustomerModel
            ->alias('potential')
            ->join('__USER__ user','potential.lock_uid = user.id')
            ->join('__DEPARTMENT_USER__ dep_user','potential.lock_uid = dep_user.user_id','left')
            ->join('__DEPARTMENT__ dep','dep_user.dep_id = dep.id','left')
            ->field('user.user_nickname,count(potential.id) data,dep.name,potential.lock_uid')
            ->where($where)
            ->group('potential.lock_uid')
            ->order('data desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('where_r', $where_r);
        $this->assign('lists', $lists);
        $this->assign('page', $page);
        return $this->fetch();
    }

    function detail()
    {
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();

        $id = $this->request->param('id',0,'intval');
        if(!$id) {
            $this->error('参数错误！');
        }
        $where = [
            'lock_uid' => $id,
        ];
        if (input('keyword')) {
            $keyword = input('keyword');
            $where['company'] = ['like', "%$keyword%"];
//            $where['company|corporation|address|email|phone|remark|scope'] = ['like', "%$keyword%"];
        }

        $lists = $potentialCustomerModel
            ->alias('potential')
            ->join('__CURRENCY__ currency', 'potential.currency = currency.id')
            ->field('potential.*,currency.name cname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('where_r', $where_r);
        $this->assign('lists', $lists);
        $this->assign('page', $page);
        return $this->fetch();
    }

    function product_select()
    {
        $potentialCustomerModel = new RongcuiPotentialCustomerModel();

        $where = [
            'type' => ['neq', 0],
            'done' => 0,
            'product_sh' => 2,
            'lock_uid' => cmf_get_current_admin_id(),
            'abutment' => 0
        ];
//        $uid = cmf_get_current_admin_id();
//        if ($uid != 1) {
//            $roles = db('role_user')
//                ->where(['user_id' => $uid])
//                ->column('role_id');
//
//            $role_deps = get_show_department($roles);
//            $users = [];
//            foreach ($role_deps as $role_dep) {
//                if ($role_dep['show_department']) {
//                    $gids = getGroups($role_dep['id']);
//                    $users[] = getUsers($gids);
//                } else {
//                    $users[] = $uid;
//                }
//            }
//
//            $users = implode(',', $users);
//            $where['lock_uid'] = ['in', $users];
//        } else {
//            $where['lock_uid'] = ['neq', 0];
//        }

        $lists = $potentialCustomerModel
            ->alias('potential')
            ->where($where)
            ->select();

        $this->assign('lists', $lists);
        return $this->fetch();
    }
}